import 'dart:async';
import 'dart:convert';

import 'package:city_pickers/city_pickers.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common.dart';

class MallUpAddrerss extends StatefulWidget {
  @override
  State<MallUpAddrerss> createState() => _SettingPageState();
}

class _SettingPageState extends State<MallUpAddrerss> {
  final TextEditingController name = TextEditingController(text: '');
  final TextEditingController phone = TextEditingController(text: '');
  final TextEditingController address = TextEditingController(text: '');
  final TextEditingController minaddress = TextEditingController(text: '');

  final edit = Get.parameters['edit'] ?? '';
  final ress = Get.parameters['address'] ?? '';

  bool _switchValue = false;
  String province = '';
  String city = '';
  String district = '';

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (edit == '1') {
      var a = jsonDecode(ress);
      name.text = a['consignee'];
      phone.text = a['phone'];
      minaddress.text = a['address'];
      address.text = '${a['province']} ${a['city']} ${a['district']}';
      province = a['province'];
      city = a['city'];
      district = a['district'];
      _switchValue = a['is_default'] == 1 ? true : false;
    }
  }

  @override
  void dispose() {
    name.dispose();
    phone.dispose();
    address.dispose();
    minaddress.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(0xffffffff),
        title: Text('收货地址'),
        centerTitle: true,
        leading: buildBackButton(context),
      ),
      body: Column(
        children: [
          Container(
            margin: EdgeInsets.symmetric(horizontal: 15),
            padding: EdgeInsets.symmetric(horizontal: 15),
            decoration: BoxDecoration(
              color: Color(0XFFF8F9FD),
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    Container(
                      width: 60,
                      child: Text(
                        '收货人',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF0F172B),
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextField(
                        controller: name,
                        style: TextStyle(fontSize: 14),
                        decoration: InputDecoration(
                          isCollapsed: true, //设置高度
                          contentPadding: EdgeInsets.all(15),
                          hintStyle: TextStyle(color: Color(0xff999999)),
                          border: InputBorder.none, // 隐藏输入框默认边框
                          hintText: '请输入真实姓名', // 提示文本
                        ),
                      ),
                    ),
                  ],
                ),
                Row(
                  children: [
                    Container(
                      width: 60,
                      child: Text(
                        '手机号码',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF0F172B),
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextField(
                        controller: phone,
                        style: TextStyle(fontSize: 14),
                        decoration: InputDecoration(
                          isCollapsed: true, //设置高度
                          contentPadding: EdgeInsets.all(15),
                          hintStyle: TextStyle(color: Color(0xff999999)),
                          border: InputBorder.none, // 隐藏输入框默认边框
                          hintText: '请输入手机号码', // 提示文本
                        ),
                      ),
                    ),
                  ],
                ),
                GestureDetector(
                  onTap: () async {
                    Result? result = await CityPickers.showCityPicker(
                      context: context,
                      theme: Theme.of(context),
                      cancelWidget: Text(
                        '取消',
                        style: TextStyle(color: Colors.red),
                      ),
                      confirmWidget: Text(
                        '确定',
                        style: TextStyle(color: Colors.blue),
                      ),
                    );

                    if (result != null) {
                      province = '${result.provinceName}';
                      city = '${result.cityName}';
                      district = '${result.areaName}';

                      setState(() {
                        address.text =
                            "${result.provinceName} ${result.cityName} ${result.areaName}";
                      });
                    }
                  },
                  child: Row(
                    children: [
                      Container(
                        width: 60,
                        child: Text(
                          '所在地区',
                          style: TextStyle(
                            fontSize: 14,
                            color: Color(0XFF0F172B),
                          ),
                        ),
                      ),
                      Expanded(
                        child: TextField(
                          controller: address,
                          style: TextStyle(fontSize: 14),
                          decoration: InputDecoration(
                            isCollapsed: true, //设置高度
                            contentPadding: EdgeInsets.all(15),
                            hintStyle: TextStyle(color: Color(0xff999999)),
                            border: InputBorder.none, // 隐藏输入框默认边框
                            hintText: '省、市、区、街道', // 提示文本
                          ),
                        ),
                      ),
                      Icon(
                        Icons.location_on,
                        color: Color(0XFF999999),
                        size: 13,
                      ),
                      Text(
                        '请选择',
                        style: TextStyle(
                          fontSize: 12,
                          color: Color(0XFF999999),
                        ),
                      ),
                    ],
                  ),
                ),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 60,
                      padding: EdgeInsets.only(top: 12),
                      child: Text(
                        '详细地址',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF0F172B),
                        ),
                      ),
                    ),
                    Expanded(
                      child: TextField(
                        controller: minaddress,
                        maxLines: null,
                        minLines: 3,
                        style: TextStyle(fontSize: 14),
                        decoration: InputDecoration(
                          isCollapsed: true, //设置高度
                          contentPadding: EdgeInsets.all(15),
                          hintStyle: TextStyle(color: Color(0xff999999)),
                          border: InputBorder.none, // 隐藏输入框默认边框
                          hintText: '小区楼栋/乡村名称', // 提示文本
                        ),
                      ),
                    ),
                  ],
                ),

                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      width: 60,
                      padding: EdgeInsets.only(top: 12),
                      child: Text(
                        '是否默认',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0XFF0F172B),
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.only(top: 3),
                      child: Transform.scale(
                        scale: 0.6, // 缩放比例
                        child: Switch(
                          value: _switchValue,
                          onChanged: (bool value) {
                            setState(() {
                              _switchValue = value;
                            });
                          },
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
          Expanded(child: SizedBox()),
          GestureDetector(
            onTap: () {
              if (name.text.isEmpty) {
                showTost('填写收货人');
                return;
              }
              if (phone.text.isEmpty) {
                showTost('填写手机号');
                return;
              }
              if (address.text.isEmpty) {
                showTost('选择地区');
                return;
              }
              if (minaddress.text.isEmpty) {
                showTost('填写地址');
                return;
              }

              postData(
                url: '/addresses',
                data: {
                  "province": province,
                  "city": city,
                  "district": district,
                  "address": minaddress.text,
                  "consignee": name.text,
                  "phone": phone.text,
                  "is_default": _switchValue ? "1" : '0',
                },
              ).then((res) {
                if (res != null) {
                  showTost('添加成功');

                  Timer(Duration(seconds: 1), () {
                    back();
                  });
                }
              });
            },
            child: Container(
              margin: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
              height: 50,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Color(0XFF0F172B),
                borderRadius: BorderRadius.circular(50),
              ),
              child: Text(
                '确定',
                style: TextStyle(fontSize: 18, color: Color(0XFFFFFFFF)),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
